/**
 * @description: 授权中心配置
 * @version: 0.0.1
 * @author: z..
 * @Date: 2023-06-12 18:16:39
 */

import { Provide } from '@midwayjs/core';
import { AuthCenterConfig } from './auth_center_config.entity';

import { InjectRepository, InjectDataSource } from '@midwayjs/sequelize';
import { Repository, Sequelize } from 'sequelize-typescript';

@Provide()
export class AuthCenterConfigModel {

  @InjectRepository(AuthCenterConfig)
  AuthCenterConfigRepository: Repository<AuthCenterConfig>;

  @InjectDataSource()
  SequelizeQuery: Sequelize

  /**
   * 新增数据
   * @param {*} appName 
   * @param {*} appCode 
   * @param {*} callbackUrl
   * @returns 
   */
  async insert(params: any): Promise<any> {
    let sql = `INSERT INTO auth_center_config 
        SET app_name = '${params.appName}',
            app_code = '${params.appCode}',
            callback_url = '${params.callbackUrl}',
            created_at = now(),
            updated_at = now();`;
    const [results] = await this.SequelizeQuery.query(sql);
    return results;
  }
  /**
   * 根据code查询数据
   * @param {*} code 
   * @returns 
   */
  async getDataByCode(code: string): Promise<object> {
    const sql = `SELECT id,app_name,app_code,callback_url FROM auth_center_config WHERE app_code = '${code}';`;
    const [results] = await this.SequelizeQuery.query(sql);
    return results;
  };

  /**
    * 根据id更新数据
    * @param {*} id 
    * @param {*} appName 
    * @param {*} callbackUrl
    * @returns 
    */
  async updateTokenById(params: any): Promise<any> {
    const sql = `UPDATE auth_center_config 
      SET app_name = '${params.appName}',
          callback_url = '${params.callbackUrl}',
          updated_at = now()
        WHERE
            id = ${params.id}`;
    const [results] = await this.SequelizeQuery.query(sql);
    return results;
  }

  /**
    * 删除
    * @param {*} id
    * @returns 
    */
  async deleteById(id: number): Promise<any> {
    return await this.AuthCenterConfigRepository.destroy({ where: { id: id } })
  }

  /**
   * 分页查询获取总条数
   * @param {*} searchKey   搜索信息
   * @returns 
   */
  async getTotalNum(searchKey: any): Promise<any> {
    let sql = `SELECT count(1) as num from auth_center_config;`;
    if (searchKey) {
      sql = `SELECT count(1) AS num FROM auth_center_config WHERE app_name LIKE '%${searchKey}%';`;
    }
    const [results] = await this.SequelizeQuery.query(sql);
    return results;
  }

  /**
   * 分页查询获取列表
   * @param {*} pageNum 
   * @param {*} pageSize 
   * @param {*} searchKey 
   * @returns 
   */
  async getDataByPage(pageNum: number, pageSize: number, searchKey: any) {
    const _limit = (pageNum - 1) * pageSize;
    let sql = `SELECT id,app_name,app_code,callback_url,created_at,updated_at FROM auth_center_config ORDER BY created_at DESC LIMIT ${_limit},${pageSize};`;
    if (searchKey) {
      sql = `SELECT id,app_name,app_code,callback_url,created_at,updated_at FROM auth_center_config WHERE app_name LIKE '%${searchKey}%' ORDER BY created_at DESC LIMIT ${_limit},${pageSize};`;
    }
    const [results] = await this.SequelizeQuery.query(sql);
    return results;
  }

  /**
   * 查询
   * @param {*} code   编码
   * @returns 
   */
  async getNumByCode(code: string, id: any): Promise<any> {
    let sql = `SELECT count(1) as num from auth_center_config where app_code = '${code}';`;
    if (id) {
      sql = `SELECT count(1) as num from auth_center_config where app_code = '${code}' and id != ${id};`;
    }
    const [results] = await this.SequelizeQuery.query(sql);
    return results;
  }

}